/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JIFRegistrarAluguel.java
 *
 * Created on 18/06/2011, 10:44:55
 */
package br.unesp.demac.bcc.soo.view.relatorio;

import br.unesp.demac.bcc.soo.bean.Contato;
import br.unesp.demac.bcc.soo.bean.Endereco;
import br.unesp.demac.bcc.soo.bean.Pessoa;
import br.unesp.demac.bcc.soo.dao.PessoaDAO;
import br.unesp.demac.bcc.soo.dao.PessoaDAOImp;
import br.unesp.demac.bcc.soo.util.Dialog;
import br.unesp.demac.bcc.soo.wsClient.AluguelRel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author flavioam
 */
public class JIFFilmesEmAtraso extends javax.swing.JInternalFrame {

    /** Creates new form JIFRegistrarAluguel */
    public JIFFilmesEmAtraso() {
        initComponents();
        populaClientes();
    }

    private void populaClientes() {
        try {
            PessoaDAO pDao = new PessoaDAOImp();

            ArrayList<Pessoa> l;

            l = pDao.selecionaTodasPessoas();

            Pessoa pe = new Pessoa();
            pe.setNome("Todos");
            pe.setEndereco(new Endereco());
            pe.setContato(new Contato());
            comboNome.addItem(pe);

            for (Pessoa p : l) {

                comboNome.addItem(p);
            }
        } catch (Exception ex) {
            Dialog.errorDialog(ex.getMessage());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtCpfCliente = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtTelResid = new javax.swing.JTextField();
        txtEnd = new javax.swing.JTextField();
        txtTelCel = new javax.swing.JTextField();
        comboNome = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaAluguel = new javax.swing.JTable();

        setClosable(true);
        setTitle("Relatório: Filmes em Atraso");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unesp/demac/bcc/soo/img/ico16x16/outIcon.png"))); // NOI18N
        setPreferredSize(new java.awt.Dimension(800, 699));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cliente"));

        jLabel3.setText("Cpf: ");

        txtCpfCliente.setEnabled(false);

        jLabel4.setText("Tel. Resid.:");

        jLabel5.setText("Tel. Cel.:");

        jLabel6.setText("End.:");

        txtTelResid.setEnabled(false);
        txtTelResid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTelResidActionPerformed(evt);
            }
        });

        txtEnd.setEnabled(false);

        txtTelCel.setEnabled(false);

        comboNome.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                comboNomeItemStateChanged(evt);
            }
        });
        comboNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboNomeActionPerformed(evt);
            }
        });

        jLabel7.setText("Cliente:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(30, 30, 30)
                        .addComponent(comboNome, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6))
                        .addGap(41, 41, 41)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtEnd, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                            .addComponent(txtCpfCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(103, 103, 103)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel4)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtTelResid, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTelCel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(13, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(comboNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtCpfCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtTelResid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtTelCel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Relatório"));

        tabelaAluguel.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cliente", "Dvd", "Gênero", "Modalidade", "Data Locação", "Data Devolução", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaAluguel.setEnabled(false);
        jScrollPane1.setViewportView(tabelaAluguel);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 732, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtTelResidActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTelResidActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTelResidActionPerformed

    private void comboNomeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboNomeItemStateChanged
        // TODO add your handling code here:
        Pessoa p = (Pessoa) comboNome.getSelectedItem();

        txtCpfCliente.setText(p.getCpf());
        txtEnd.setText(p.getEndereco().getEndereco());
        txtTelCel.setText(p.getContato().getTelefoneCelular());
        txtTelResid.setText(p.getContato().getTelefoneResidencial());

        br.unesp.demac.bcc.soo.wsClient.Pessoa pWs = new br.unesp.demac.bcc.soo.wsClient.Pessoa();
        pWs.setNome(p.getNome());
        pWs.setCpf(p.getCpf());
        pWs.setIdPessoa(p.getIdPessoa());

        List<AluguelRel> l = relatorioAtrasoCliente(pWs);

        DefaultTableModel model = (DefaultTableModel) tabelaAluguel.getModel();
        model.setNumRows(0);
        SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy");

        for (AluguelRel aRel : l) {

            Object[] newRow = new Object[7];

            newRow[0] = aRel.getPessoa().getNome();
            newRow[1] = aRel.getDvd().getNome();
            newRow[2] = aRel.getDvd().getGenero();
            newRow[3] = aRel.getDvd().getModalidade().getNome();
            newRow[4] = sd.format(aRel.getAluguel().getDataLocacao().toGregorianCalendar().getTime());
            newRow[5] = sd.format(aRel.getDataDevolucao().toGregorianCalendar().getTime());
            newRow[6] = aRel.getValor();

            model.addRow(newRow);
        }
    }//GEN-LAST:event_comboNomeItemStateChanged

    private void comboNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboNomeActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_comboNomeActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboNome;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelaAluguel;
    private javax.swing.JTextField txtCpfCliente;
    private javax.swing.JTextField txtEnd;
    private javax.swing.JTextField txtTelCel;
    private javax.swing.JTextField txtTelResid;
    // End of variables declaration//GEN-END:variables

    private static java.util.List<br.unesp.demac.bcc.soo.wsClient.AluguelRel> relatorioAtrasoCliente(br.unesp.demac.bcc.soo.wsClient.Pessoa pessoa) {
        br.unesp.demac.bcc.soo.wsClient.LocadoraWS_Service service = new br.unesp.demac.bcc.soo.wsClient.LocadoraWS_Service();
        br.unesp.demac.bcc.soo.wsClient.LocadoraWS port = service.getLocadoraWSPort();
        return port.relatorioAtrasoCliente(pessoa);
    }
}
